//
//  HealthViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "HealthViewController.h"
#import "HealthModel.h"
#import "HealthTableViewCell.h"
#import "MJRefresh.h"
#define kUrl @"http://ibaby.ipadown.com/api/food/food.show.list.php?keywords=%E5%A5%B3%E6%80%A7&p=1&pagesize=20&from=com.ipadown.nxsp&version=2.0"
#define kurl @"http://ibaby.ipadown.com/api/food/food.show.list.php?keywords=%E5%A5%B3%E6%80%A7&p="


@interface HealthViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSMutableArray *allArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, assign) NSInteger p;
@end

@implementation HealthViewController

- (NSMutableArray *)allArr
{
    if (!_allArr) {
        _allArr = [NSMutableArray array];
    }
    return _allArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    [self getData];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    

    
}
- (void)getMoredata
{
    int p = 1;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *url = [kurl stringByAppendingString:[NSString stringWithFormat:@"%d&pagesize=20&from=com.ipadown.nxsp&version=2.0",++p]];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *arr = dic[@"results"];
        for (NSDictionary *dictionary in arr) {
            HealthModel *model = [[HealthModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.allArr addObject:model];
        }
        [self.tableView footerEndRefreshing]; // 停止刷新
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];

    
    
    
}
- (void)cancel
{
    //[self.tableView reloadData];
   // [self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>];
    [self.webView removeFromSuperview];
    
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HealthTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    __block HealthViewController *vc = self;
    [self.tableView addFooterWithCallback:^{
        [vc getMoredata];
    }];
    
    

    
}
- (void)getData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *arr = dic[@"results"];
        for (NSDictionary *dictionary in arr) {
            HealthModel *model = [[HealthModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.allArr addObject:model];
        }
        [self creatTableView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allArr.count;
    
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     HealthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     HealthModel *model = [self.allArr objectAtIndex:indexPath.row];
     [cell.thumb sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
     [cell setModel:model];
     
     
     return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HealthModel *model = [self.allArr objectAtIndex:indexPath.row];
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    NSString *str = [@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=" stringByAppendingString:model.ID];
    NSURL *url = [NSURL URLWithString:str];
    [self.webView loadRequest:[[NSURLRequest alloc]initWithURL:url]];
    [self.view addSubview:self.webView];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
